package me.helllp.dake.core.component;

import me.helllp.dake.core.DakeCommon;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * 国际化处理
 */
@Component("localeResolver")
public class DakeLocaleResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String l = request.getParameter(DakeCommon.QUERY_I18N_KEY);
        if (StringUtils.isEmpty(l)) {
            // 路径上没有国际化语言参数，采用默认的（从请求头中获取）
            return request.getLocale();
        } else {
            String[] split = l.split("_");
            //语言、国家构造器
            return new Locale(split[0], split[1]);
        }
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
        throw new UnsupportedOperationException("Cannot change Dake locale Resolver...");
    }
}
